10. Pass by Reference in C++
Pass by Reference
In the previous exercises, you've written functions that accept and return various kinds of objects. However, in all of the functions you've written so far, the objects returned by the function are different from the objects provided to the function. In other words, when the function is called on some data, a copy of that data is made, and the function operates on a copy of the data instead of the original data. This is referred to as pass by value, since only a copy of the values of an object are passed to the function, and not the actual objects itself.
To see how to use a function to operate directly on a given object, have a look at the notebook below.
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: jupyter
- Opened files (when workspace is loaded): n/a
On to the Exercises!
In the next exercises you will write some functions that require passing variables by reference. This will allow helper functions in the A* search program to modify the state of the board without having to copy the entire board, for instance.
Note that if you've encountered references before in C++, you are aware that they can be used in many other scenarios, aside from just passing variables to functions. In the next lessons, you will learn about references more generally, along with closely related pointers. However, you now know enough to finish the mini-project for the first half of the course!
Since you are well prepared with all you need at this point, this will be the last notebook in this half of the course. Good luck!